package com.soul.common.msg;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author grass
 * @version 1.0
 * @date 2022/9/5 14:36
 */
@Data
@ApiModel(description = "返回信息")
@Accessors(chain = true) // 使用链式创建，setter方法返回的是this
public class ApiResult<T> {

    @ApiModelProperty(value = "状态码",required = true)
    private Integer status = 200;

    @ApiModelProperty(value = "消息",required = true)
    private String message;
    @ApiModelProperty(value = "返回详细信息")
    private String detailedMessage;
    @ApiModelProperty(value = "是否成功",required = true)
    private boolean success = true;

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "数据总数")
    private Long total;

    public ApiResult(){

    }
    public ApiResult(ResultEnum resultEnum){
        this.status = resultEnum.getStatus();
        this.message = resultEnum.getMsg();
        this.success = resultEnum.isSuccess();
    }

    public ApiResult(ResultEnum resultEnum, String detailedMessage, T data, Long total) {
        this.status = resultEnum.getStatus();
        this.message = resultEnum.getMsg();
        this.success = resultEnum.isSuccess();

        this.detailedMessage = detailedMessage;
        this.data = data;
        this.total = total;
    }

    public ApiResult(int status, String message, String detailedMessage) {
        this.status = status;
        this.message = message;
        if (status!=200){
            this.success = false;
        }
        this.detailedMessage = detailedMessage;
    }
    public ApiResult(int status, String message) {
        this.status = status;
        this.message = message;
        if (status!=200){
            this.success = false;
        }
    }

    /**
     * 统一返回 请求成功
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> success(){
        return success(null);
    }

    /**
     * 统一返回 请求成功并承载数据
     * @param data
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> success(E data){
        return success(data,null);
    }

    /**
     * 统一返回 请求成功并承载分页数据
     * @param data
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> success(E data, Long total){
        ApiResult<E> r = new ApiResult<>(ResultEnum.SUCCESS);
        r.data = data;
        r.setTotal(total);
        return r;
    }
    /**
     * 统一返回 请求系统错误
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> error(){
        return error("");
    }

    /**
     * 统一返回 请求系统错误
     * @param detailedMessage
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> error(String detailedMessage){
        ApiResult<E> r = new ApiResult<>(ResultEnum.ERROR);
        r.setDetailedMessage(detailedMessage);
        return r;
    }

    /**
     * 统一返回 请求系统错误
     * @param detailedMessage
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> error(int status,String mes,String detailedMessage){
        return new ApiResult<>(status,mes,detailedMessage);
    }

    /**
     * 统一返回 请求系统错误
     * @param status
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> error(int status,String mes){
        return new ApiResult<>(status,mes);
    }

    /**
     * 统一返回 自定义状态码
     * @param resultEnum
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> result(ResultEnum resultEnum){
        return result(resultEnum,"");
    }

    /**
     * 统一返回 自定义状态
     * @param resultEnum
     * @param detailedMessage
     * @return
     * @param <E>
     */
    public static <E> ApiResult<E> result(ResultEnum resultEnum, String detailedMessage){
        ApiResult<E> r = new ApiResult<>(resultEnum);
        r.setDetailedMessage(detailedMessage);
        return r;
    }

}
